package com.fitness.front.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.front.convertor.CoachConvertor;
import com.fitness.front.modal.coach.dto.CoachDto;
import com.fitness.front.modal.coach.entity.Coach;
import com.fitness.front.modal.coach.param.CoachPageQueryParams;
import com.fitness.front.modal.coach.vo.CoachVO;
import com.fitness.front.modal.course.entity.CourseSchedule;
import com.fitness.front.modal.course.vo.CourseScheduleVO;
import com.fitness.front.modal.course.vo.CourseVO;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.ICoachService;
import com.fitness.front.service.ICourseScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/front/coach")
@RequiredArgsConstructor
public class CoachController {

    private final ICoachService coachService;
    private final CoachConvertor coachConvertor;
    private final ICourseScheduleService courseScheduleService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<CoachVO>> page(CoachPageQueryParams queryParams) {
        Page<CoachVO> result = coachService.getCoachPage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param coachDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<CoachVO> add(@RequestBody CoachDto coachDto) {
        boolean result = coachService.addCoach(coachDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10001","添加教练失败");
    }

    /**
     * 修改
     * @param coachDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<CoachVO> update(@RequestBody CoachDto coachDto) {
        boolean result = coachService.updateCoach(coachDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10002","更新教练失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(Long id) {
        boolean result = coachService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("C10003","删除教练失败");
    }

    /**
     * 分页获取
     */
    @GetMapping("/getCoach")
    public ServerResponseEntity<CoachVO> getCoach(Long id) {
        Coach result = coachService.getById(id);
        return ServerResponseEntity.success(coachConvertor.entity2Vo(result));
    }

    @GetMapping("/scheduleCourses")
    public ServerResponseEntity<List<CourseVO>> scheduleCourses(Long coachId){
        List<CourseVO> scheduleList = courseScheduleService.getCourseScheduleByCoachId(coachId);
        return ServerResponseEntity.success(scheduleList);
    }
}
